
// 导入第三方包到程序中
let express = require("express")
// 1.创建web应用
let app = express()
// 2. 创建静态资源服务器
//app.use 注册中间件，express.static  托管静态资源的中间件
app.use(express.static('static'))


//======设置中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


//定义服务器数据
let arr = [
    { email: '138284773@qq.com', name: '王一', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '278884773@qq.com', name: '王二', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: true, status: false },
    { email: '2123184773@qq.com', name: '王三', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '174555553@qq.com', name: '王四', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '1781234773@qq.com', name: '王五', phone: '13611129070', pass: '1234', ingroup: '测试组', isadmin: false, status: true },
    { email: '28214773@qq.com', name: '王六', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: false, status: false },
    { email: '17823184773@qq.com', name: '王琦', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: 'ffffgrrr@qq.com', name: '王八', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '1712384773@qq.com', name: '王九', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '3456655@qq.com', name: '李一', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '378884773@qq.com', name: '李二', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: false, status: false },
    { email: '34445@qq.com', name: '李三', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: true, status: false },
    { email: '44555@qq.com', name: '李四', phone: '13611129070', pass: '1234', ingroup: '测试组', isadmin: true, status: true },
    { email: '378884773@qq.com', name: '李五', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: '47844555884773@qq.com', name: '李六', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '17488444773@qq.com', name: '李七', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: true, status: false },
    { email: '175ert3@qq.com', name: '李八', phone: '13611129070', pass: '1234', ingroup: '测试组', isadmin: true, status: false },
    { email: '3488333@qq.com', name: '李达', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: '678884773@qq.com', name: '阿萨', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: true, status: false },
    { email: '178884773@qq.com', name: '李清照', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: false, status: false },
    { email: 'qwwe1234@qq.com', name: '阿萨', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '1234rfvr@qq.com', name: '纷纷', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: false },
    { email: '1344rf@qq.com', name: '阿斯达', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: false, status: true },
    { email: '234rff@qq.com', name: '阿斯达', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: '3erff@qq.com', name: '阿萨', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: true, status: false },
    { email: '2456@qq.com', name: '纷纷', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: false },
    { email: '24rfg@qq.com', name: '张伟', phone: '13611129070', pass: '1234', ingroup: '测试组', isadmin: false, status: true },
    { email: '2566hg@qq.com', name: '李煜', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: '1345t@qq.com', name: '李达', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: false, status: true },
    { email: '234fgt@qq.com', name: '纷纷', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '138284773@qq.com', name: '张一', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '278884773@qq.com', name: '张二', phone: '13611129070', pass: '1234', ingroup: '前端组', isadmin: true, status: false },
    { email: '2123184773@qq.com', name: '张三', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '174555553@qq.com', name: '张四', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '1781234773@qq.com', name: '张五', phone: '13611129070', pass: '1234', ingroup: '测试组', isadmin: false, status: true },
    { email: '28214773@qq.com', name: '张六', phone: '13611129070', pass: '1234', ingroup: '后端组', isadmin: false, status: false },
    { email: '17823184773@qq.com', name: '张琦', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: false, status: true },
    { email: 'ffffgrrr@qq.com', name: '张八', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '1712384773@qq.com', name: '张九', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true }
]



//===设置接口--翻页操作
app.get('/user/list', (req, res) => {//req（接受客户端发来的请求信息，地址、方式、参数）    res  服务器返回的信息，json格式数据
    // 保存客户端传来的参数
    let { page, count } = req.query
    // 截取服务器数据，并按照请求参数返回一定个数的数据
    let newarr = arr.slice((page - 1) * count, page * count);
    // 返回数据
    res.send({ code: 200, msg: '请求成功', list: newarr });
})

//===模糊搜索接口
app.get('/user/search', (req, res) => {
    //接收参数
    let { searchkey } = req.query;
    //根据参数,从用户的name属性中过滤
    let newarr = arr.filter((item) => {
        return item.name.includes(searchkey)
    })
    //返回数据
    res.send({ code: 200, msg: '查询成功!', list: newarr })
})

//===登录接口
app.post('/user/login', (req, res) => {

    //想要接收post类型请求的参数 必须提前设置能解析post请求参数的中间件 express.json 和 express.urlencoded 
    let { phone, pass } = req.body;
    console.log({ phone, pass });

    //用来自客户端的数据 做判断, 判断是否在服务端的用户列表中
    let i = arr.findIndex((item, index) => {
        return item.phone == phone && item.pass == pass;
    })

    //如果i不是-1, 说明来自客户端的数据是合法的.
    if (i != -1) {
        //登陆成功时, 可以一并返回 登陆成功的用户信息,登陆令牌
        res.send({ code: 200, msg: '登陆成功!', userinfo: arr[i], token: '' });
    } else {
        res.send({ code: 400, msg: '登陆失败!' });
    }
})

// 4. 设置监听端口
app.listen(8080)